package com.app.bimo.commonbusiness.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * Created by limo on 16/2/18.
 */
public class TimeUnit {

    public static final int SECONDS_IN_DAY = 60 * 60 * 24;
    public static final long MILLIS_IN_DAY = 1000L * SECONDS_IN_DAY;

    public static boolean isSameDayOfMillis(long ms1, long ms2) {
        final long interval = ms1 - ms2;
        return interval < MILLIS_IN_DAY
                && interval > -1L * MILLIS_IN_DAY
                && toDay(ms1) == toDay(ms2);
    }

    private static long toDay(long millis) {
        return (millis + TimeZone.getDefault().getOffset(millis)) / MILLIS_IN_DAY;
    }


    public static boolean isSameDate(long ms1, long ms2) {
        //  long temp=ms1-ms2;
        /**
         * 修正偏差
         */
        return (ms1 + 28800) / 86400 == (ms2 + 28800) / 86400;
//        if(temp<86400&&temp>-86400){
//            return true;
//        }
//        return false;
//        String d1 = TimeStamp2Date(String.valueOf(ms1), "yyyy-MM-dd");
//        String d2 = TimeStamp2Date(String.valueOf(ms2), "yyyy-MM-dd");
////        Log.e("date", d1 + "--------" + d2);
//        if (d1.equals(d2)) {
//            return true;
//        } else {
//            return false;
//        }
    }

    public static String getTimeByTimeMillis(long timeMillis) {
        long now = System.currentTimeMillis() / 1000;
        long l = now - timeMillis;
        long day = l / (24 * 60 * 60);
        long hour = l / (60 * 60);
        long min = l / 60;
        long s = l;
        if (day > 0)
            return (day + "天前");
        if (hour > 0)
            return hour + "小时前";
        if (min > 0)
            return min + "分钟前";
        if (s < 0) {
            return "0秒前";

        }
        return s + "秒前";
    }

    public static boolean isSameYear(long ms1, long ms2) {
        /**
         * 修正偏差
         */
        String d1 = TimeStamp2Date(String.valueOf(ms1), "yyyy");
        String d2 = TimeStamp2Date(String.valueOf(ms2), "yyyy");
        return d1.equals(d2);
    }

    public static boolean isSameMonth(long ms1, long ms2) {
        String d1 = TimeStamp2Date(String.valueOf(ms1), "yyyy-MM");
        String d2 = TimeStamp2Date(String.valueOf(ms2), "yyyy-MM");
        return d1.equals(d2);
    }

    public static String TimeStamp2Date(String timestampString, String formats) {
        Long timestamp = Long.parseLong(timestampString) * 1000;
        String date = new SimpleDateFormat(formats).format(new Date(timestamp));
        return date;
    }

    public static String CaculateWeekDay(int y, int m, int d) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, y);
        calendar.set(Calendar.MONTH, m);
        calendar.set(Calendar.DAY_OF_MONTH, d);
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        String weekstr = "";
        switch (week) {
            case 2:
                weekstr = "星期一";
                break;
            case 3:
                weekstr = "星期二";
                break;
            case 4:
                weekstr = "星期三";
                break;
            case 5:
                weekstr = "星期四";
                break;
            case 6:
                weekstr = "星期五";
                break;
            case 7:
                weekstr = "星期六";
                break;
            case 1:
                weekstr = "星期天";
                break;
        }

        return weekstr;
    }

    public static int getWeek(long time) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(time * 1000);
        int week = c.get(Calendar.DAY_OF_WEEK);
        return week;
    }

    /**
     * 通过时间秒毫秒数判断两个时间的间隔
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDaysByMillisecond(long date1, long date2) {
        int days = (int) ((date1 - date2) / (3600 * 24));
        return days;
    }

    public static long stringFromat(String format, String time) {
        if (time == null) {
            return -1;
        }
        try {
            Date date = new SimpleDateFormat(format).parse(time);
            return date.getTime();
        } catch (Exception e) {

        }
        return -1;
    }


    /**
     * start
     * 本周开始时间戳
     */
    public static String getWeekStartTime() {
        Calendar cal = Calendar.getInstance();
        // 获取星期日开始时间戳
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return (cal.getTimeInMillis()/1000)+"";
    }

    /**
     * end
     * 本周结束时间戳
     */
    public static String getWeekEndTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd", Locale.getDefault());
        Calendar cal = Calendar.getInstance();
        // 获取星期六结束时间戳
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
        return simpleDateFormat.format(cal.getTime()) + "235959999";
    }

    //获得当天0点时间
    public static int getTimesmorning(){

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);

        return (int) (cal.getTimeInMillis()/1000);
    }

}
